package main

import (
	pb "demo/15/proto/model"
	"fmt"
	"google.golang.org/grpc/credentials/insecure"
	"time"

	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

func main10() {
	// 连接grpc服务器
	credentials := grpc.WithTransportCredentials(insecure.NewCredentials())
	conn, err := grpc.Dial("127.0.0.1:8090", credentials)
	if err != nil {
		fmt.Printf("连接失败: %v\n", err)
	}
	// 延迟关闭连接
	defer conn.Close()

	// 初始化UserService客户端
	userClient := pb.NewUserServiceClient(conn)

	// 定义超时上下文
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 远程调用服务端方法
	response, err := userClient.GetUserInfo(ctx, &pb.UserRequest{Name: "golang"})
	if err != nil {
		fmt.Printf("调用GetUserInfo失败: %v\n", err)
		return
	}

	fmt.Printf("用户ID：%+v\n", response.Id)
	fmt.Printf("用户名：%+v\n", response.Username)
	fmt.Printf("用户昵称：%+v\n", response.Nickname)
}
